home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Gnuplot / Source / plot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-18  |  12.3 KB  |  482 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: plot.c%v 3.38.2.78 1993/02/20 02:59:43 woo Exp woo $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - plot.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software is provided "as is" without express or implied warranty.
  21.  * 
  22.  *
  23.  * AUTHORS
  24.  * 
  25.  *   Original Software:
  26.  *     Thomas Williams,  Colin Kelley.
  27.  * 
  28.  *   Gnuplot 2.0 additions:
  29.  *       Russell Lang, Dave Kotz, John Campbell.
  30.  *
  31.  *   Gnuplot 3.0 additions:
  32.  *       Gershon Elber and many others.
  33.  * 
  34.  * Send your comments or suggestions to 
  35.  *  info-gnuplot@dartmouth.edu.
  36.  * This is a mailing list; to join it send a note to 
  37.  *  info-gnuplot-request@dartmouth.edu.  
  38.  * Send bug reports to
  39.  *  bug-gnuplot@dartmouth.edu.
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <setjmp.h>
  44. #include <signal.h>
  45. #ifdef XPG3_LOCALE
  46. #include <locale.h>
  47. #endif
  48. #include "plot.h"
  49. #include "setshow.h"
  50. #if defined(MSDOS) || defined(DOS386)
  51. #include <io.h>
  52. #endif
  53. #ifdef vms
  54. #include <unixio.h>
  55. #include <smgdef.h>
  56. extern int vms_vkid;
  57. extern smg$create_virtual_keyboard();
  58. unsigned int status[2] = {1, 0};
  59. #endif
  60. #ifdef AMIGA_SC_6_1
  61. #include <proto/dos.h>
  62. #endif
  63.  
  64. #ifdef _Windows
  65. #include <windows.h>
  66. #ifndef SIGINT
  67. #define SIGINT 2    /* for MSC */
  68. #endif
  69. #else
  70. # ifdef __TURBOC__
  71. # include <graphics.h>
  72. # endif
  73. #endif
  74.  
  75. #ifndef AMIGA_SC_6_1
  76. extern char *getenv(),*strcat(),*strcpy(),*strncpy();
  77. #endif /* !AMIGA_SC_6_1 */
  78.  
  79. extern char input_line[];
  80. extern int c_token;
  81. extern FILE *outfile;
  82. extern int term;
  83.  
  84. TBOOLEAN interactive = TRUE;    /* FALSE if stdin not a terminal */
  85. TBOOLEAN noinputfiles = TRUE;    /* FALSE if there are script files */
  86. char *infile_name = NULL;    /* name of command file; NULL if terminal */
  87.  
  88. #ifndef STDOUT
  89. #define STDOUT 1
  90. #endif
  91.  
  92. #ifdef _Windows
  93. jmp_buf far env;
  94. #else
  95. jmp_buf env;
  96. #endif
  97.  
  98. struct value *Ginteger(),*Gcomplex();
  99.  
  100.  
  101. extern f_push(),f_pushc(),f_pushd1(),f_pushd2(),f_pushd(),f_call(),f_calln(),
  102.     f_lnot(),f_bnot(),f_uminus(),f_lor(),f_land(),f_bor(),f_xor(),
  103.     f_band(),f_eq(),f_ne(),f_gt(),f_lt(),
  104.     f_ge(),f_le(),f_plus(),f_minus(),f_mult(),f_div(),f_mod(),f_power(),
  105.     f_factorial(),f_bool(),f_jump(),f_jumpz(),f_jumpnz(),f_jtern();
  106.  
  107. extern f_real(),f_imag(),f_arg(),f_conjg(),f_sin(),f_cos(),f_tan(),f_asin(),
  108.     f_acos(),f_atan(),f_sinh(),f_cosh(),f_tanh(),f_int(),f_abs(),f_sgn(),
  109.     f_sqrt(),f_exp(),f_log10(),f_log(),f_besj0(),f_besj1(),f_besy0(),f_besy1(),
  110.     f_erf(), f_erfc(), f_gamma(), f_lgamma(), f_ibeta(), f_igamma(), f_rand(),
  111.     f_floor(),f_ceil(),
  112.     f_normal(), f_inverse_erf(), f_inverse_normal();   /* XXX - JG */
  113.  
  114.  
  115. struct ft_entry ft[] = {    /* built-in function table */
  116.  
  117. /* internal functions: */
  118.     {"push", f_push},    {"pushc", f_pushc},
  119.     {"pushd1", f_pushd1},    {"pushd2", f_pushd2},    {"pushd", f_pushd},
  120.     {"call", f_call},    {"calln", f_calln},    {"lnot", f_lnot},
  121.     {"bnot", f_bnot},    {"uminus", f_uminus},    {"lor", f_lor},
  122.     {"land", f_land},    {"bor", f_bor},        {"xor", f_xor},
  123.     {"band", f_band},    {"eq", f_eq},        {"ne", f_ne},
  124.     {"gt", f_gt},        {"lt", f_lt},        {"ge", f_ge},
  125.     {"le", f_le},        {"plus", f_plus},    {"minus", f_minus},
  126.     {"mult", f_mult},    {"div", f_div},        {"mod", f_mod},
  127.     {"power", f_power}, {"factorial", f_factorial},
  128.     {"bool", f_bool},    {"jump", f_jump},    {"jumpz", f_jumpz},
  129.     {"jumpnz",f_jumpnz},{"jtern", f_jtern},
  130.  
  131. /* standard functions: */
  132.     {"real", f_real},    {"imag", f_imag},    {"arg", f_arg},
  133.     {"conjg", f_conjg}, {"sin", f_sin},        {"cos", f_cos},
  134.     {"tan", f_tan},        {"asin", f_asin},    {"acos", f_acos},
  135.     {"atan", f_atan},    {"sinh", f_sinh},    {"cosh", f_cosh},
  136.     {"tanh", f_tanh},    {"int", f_int},        {"abs", f_abs},
  137.     {"sgn", f_sgn},        {"sqrt", f_sqrt},    {"exp", f_exp},
  138.     {"log10", f_log10},    {"log", f_log},        {"besj0", f_besj0},
  139.     {"besj1", f_besj1},    {"besy0", f_besy0},    {"besy1", f_besy1},
  140.         {"erf", f_erf},         {"erfc", f_erfc},       {"gamma", f_gamma},     {"lgamma", f_lgamma},
  141.         {"ibeta", f_ibeta},     {"igamma", f_igamma},   {"rand", f_rand},
  142.         {"floor", f_floor},     {"ceil", f_ceil},
  143.  
  144.     {"norm",        f_normal},              /* XXX-JG */
  145.     {"inverf",      f_inverse_erf},         /* XXX-JG */
  146.     {"invnorm",     f_inverse_normal},      /* XXX-JG */
  147.  
  148.     {NULL, NULL}
  149. };
  150.  
  151. static struct udvt_entry udv_pi = {NULL, "pi",FALSE};
  152.                                     /* first in linked list */
  153. struct udvt_entry *first_udv = &udv_pi;
  154. struct udft_entry *first_udf = NULL;
  155.  
  156.  
  157.  
  158. #ifdef vms
  159.  
  160. #define HOME "sys$login:"
  161.  
  162. #else /* vms */
  163. #if defined(MSDOS) ||  defined(AMIGA_AC_5) || defined(AMIGA_SC_6_1) || defined(ATARI) || defined(OS2) || defined(_Windows) || defined(DOS386)
  164.  
  165. #define HOME "GNUPLOT"
  166.  
  167. #else /* MSDOS || AMIGA || ATARI || OS2 || _Windows || defined(DOS386)*/
  168.  
  169. #define HOME "HOME"
  170.  
  171. #endif /* MSDOS || AMIGA || ATARI || OS2 || _Windows || defined(DOS386)*/
  172. #endif /* vms */
  173.  
  174. #if defined(unix) || defined(AMIGA_AC_5) || defined(AMIGA_SC_6_1)
  175. #define PLOTRC ".gnuplot"
  176. #else /* AMIGA || unix */
  177. #define PLOTRC "gnuplot.ini"
  178. #endif /* AMIGA || unix */
  179.  
  180. #if defined (__TURBOC__) || defined (__PUREC__)
  181. void tc_interrupt()
  182. #else
  183. #ifdef __ZTC__
  184. void ztc_interrupt()
  185. #else
  186. #if defined( _CRAY ) || defined( sgi )
  187. void inter(an_int)
  188. int an_int;
  189. #else
  190. #if defined( NEXT ) || defined( OS2 )
  191. void inter(int an_int)
  192. #else
  193. #ifdef sgi
  194. void inter(int sig, int code, struct sigcontext *sc)
  195. #else
  196. inter()
  197. #endif
  198. #endif
  199. #endif
  200. #endif
  201. #endif
  202. {
  203. #if defined (MSDOS) || defined(_Windows) || (defined (ATARI) && defined(__PUREC__)) || defined(DOS386)
  204. #if defined (__TURBOC__) || defined (__PUREC__)
  205. #ifndef DOSX286
  206.     (void) signal(SIGINT, tc_interrupt);
  207. #endif
  208. #else
  209. #ifdef __ZTC__
  210.    (void) signal(SIGINT, ztc_interrupt);
  211. #else
  212. #ifdef __EMX__
  213.     (void) signal(SIGINT, (void *)inter);
  214. #else
  215. #ifdef DJGPP
  216.     (void) signal(SIGINT, (SignalHandler)inter);
  217. #else
  218. #if defined __MSC__
  219.     (void) signal(SIGINT, inter);
  220. #endif    /* __MSC__ */
  221.  
  222. #endif    /* DJGPP */
  223. #endif  /* __EMX__ */
  224. #endif    /* ZTC */
  225. #endif  /* __TURBOC__ */
  226.  
  227. #else  /* MSDOS */
  228. #ifdef OS2
  229.     (void) signal(an_int, SIG_ACK);
  230. #else
  231.     (void) signal(SIGINT, inter);
  232. #endif  /* OS2 */
  233. #endif  /* MSDOS */
  234. #ifndef DOSX286
  235.     (void) signal(SIGFPE, SIG_DFL);    /* turn off FPE trapping */
  236. #endif
  237.     if (term && term_init)
  238.         (*term_tbl[term].text)();    /* hopefully reset text mode */
  239.     (void) fflush(outfile);
  240.     (void) putc('\n',stderr);
  241.     longjmp(env, TRUE);        /* return to prompt */
  242. }
  243.  
  244.  
  245. #ifdef _Windows
  246. gnu_main(argc, argv)
  247. #else
  248. #ifdef NEXTFE
  249. pmain(argc, argv)
  250. #else
  251. main(argc, argv)
  252. #endif
  253. #endif
  254.     int argc;
  255.     char **argv;
  256. {
  257. #ifdef XPG3_LOCALE
  258.     (void) setlocale(LC_CTYPE, "");
  259. #endif
  260. /* Register the Borland Graphics Interface drivers. If they have been */
  261. /* included by the linker.                                            */
  262.  
  263. #ifndef DOSX286
  264. #ifndef _Windows
  265. #if defined (__TURBOC__) && defined (MSDOS)
  266. registerfarbgidriver(EGAVGA_driver_far);
  267. registerfarbgidriver(CGA_driver_far);
  268. registerfarbgidriver(Herc_driver_far);
  269. registerfarbgidriver(ATT_driver_far);
  270. # endif
  271. #endif
  272. #endif
  273. #ifdef X11
  274.      { int n = X11_args(argc, argv); argv += n; argc -= n; }
  275. #endif 
  276.  
  277. #ifdef apollo
  278.     apollo_pfm_catch();
  279. #endif
  280.  
  281.     setbuf(stderr,(char *)NULL);
  282. #ifdef UNIX
  283.     setlinebuf(stdout);
  284. #endif
  285.     outfile = stdout;
  286.     (void) Gcomplex(&udv_pi.udv_value, Pi, 0.0);
  287.  
  288.      interactive = FALSE;
  289.      init_terminal();        /* can set term type if it likes */
  290.  
  291. #ifdef AMIGA_SC_6_1
  292.      if (IsInteractive(Input()) == DOSTRUE) interactive = TRUE;
  293.      else interactive = FALSE;
  294. #else
  295. #if defined(__MSC__) && defined(_Windows)
  296.      interactive = TRUE;
  297. #else
  298.      interactive = isatty(fileno(stdin));
  299. #endif
  300. #endif
  301.      if (argc > 1)
  302.       interactive = noinputfiles = FALSE;
  303.      else
  304.       noinputfiles = TRUE;
  305.  
  306.      if (interactive)
  307.       show_version();
  308. #ifdef vms   /* initialise screen management routines for command recall */
  309.           if (status[1] = smg$create_virtual_keyboard(&vms_vkid) != SS$_NORMAL)
  310.                done(status[1]);
  311. #endif
  312.  
  313.     if (!setjmp(env)) {
  314.         /* first time */
  315.         interrupt_setup();
  316.         load_rcfile();
  317.  
  318.         if (interactive && term != 0)    /* not unknown */
  319.          fprintf(stderr, "\nTerminal type set to '%s'\n", 
  320.                 term_tbl[term].name);
  321.     } else {    
  322.         /* come back here from int_error() */
  323.         load_file_error();    /* if we were in load_file(), cleanup */
  324. #ifdef _Windows
  325.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  326. #endif
  327. #ifdef vms
  328.         /* after catching interrupt */
  329.         /* VAX stuffs up stdout on SIGINT while writing to stdout,
  330.           so reopen stdout. */
  331.         if (outfile == stdout) {
  332.            if ( (stdout = freopen("SYS$OUTPUT","w",stdout))  == NULL) {
  333.               /* couldn't reopen it so try opening it instead */
  334.               if ( (stdout = fopen("SYS$OUTPUT","w"))  == NULL) {
  335.                  /* don't use int_error here - causes infinite loop! */
  336.                  fprintf(stderr,"Error opening SYS$OUTPUT as stdout\n");
  337.               }
  338.            }
  339.            outfile = stdout;
  340.         }
  341. #endif                    /* VMS */
  342.         if (!interactive && !noinputfiles) {
  343.             if (term && term_init)
  344.                 (*term_tbl[term].reset)();
  345. #ifdef vms
  346.             vms_reset();
  347. #endif
  348.             return(IO_ERROR);    /* exit on non-interactive error */
  349.          }
  350.     }
  351.  
  352.      if (argc > 1) {
  353.         /* load filenames given as arguments */
  354.         while (--argc > 0) {
  355.            ++argv;
  356.            c_token = NO_CARET; /* in case of file not found */
  357.            load_file(fopen(*argv,"r"), *argv);    
  358.         }
  359.     } else {
  360.         /* take commands from stdin */
  361.         while(!com_line());
  362.     }
  363.  
  364.     if (term && term_init)
  365.         (*term_tbl[term].reset)();
  366. #ifdef vms
  367.     vms_reset();
  368. #endif
  369.     return(IO_SUCCESS);
  370. }
  371.  
  372. #if defined(ATARI) && defined(__PUREC__)
  373. #include <math.h>
  374. int purec_matherr(struct exception *e)
  375. {    char *c;
  376.     switch (e->type) {
  377.         case DOMAIN:    c = "domain error"; break;
  378.         case SING  :    c = "argument singularity"; break;
  379.         case OVERFLOW:  c = "overflow range"; break;
  380.         case UNDERFLOW: c = "underflow range"; break;
  381.         default:        c = "(unknown error"; break;
  382.     }
  383.     fprintf(stderr, "math exception : %s\n", c);
  384.     fprintf(stderr, "    name : %s\n", e->name);
  385.     fprintf(stderr, "    arg 1: %e\n", e->arg1);
  386.     fprintf(stderr, "    arg 2: %e\n", e->arg2);
  387.     fprintf(stderr, "    ret  : %e\n", e->retval);
  388.     return 1;
  389. }
  390. #endif
  391.  
  392. /* Set up to catch interrupts */
  393. interrupt_setup()
  394. {
  395. #if defined (MSDOS) || defined(_Windows) || (defined (ATARI) && defined(__PUREC__)) || defined(DOS386)
  396. #ifdef __PUREC__
  397.     setmatherr(purec_matherr);
  398. #endif
  399. #if defined (__TURBOC__) || defined (__PUREC__)
  400. #if !defined(DOSX286) && !defined(BROKEN_SIGINT)
  401.         (void) signal(SIGINT, tc_interrupt);    /* go there on interrupt char */
  402. #endif
  403. #else
  404. #ifdef __ZTC__
  405.         (void) signal(SIGINT, ztc_interrupt);
  406. #else
  407. #ifdef __EMX__
  408.         (void) signal(SIGINT, (void *)inter);    /* go there on interrupt char */
  409. #else
  410. #ifdef DJGPP
  411.         (void) signal(SIGINT, (SignalHandler)inter);    /* go there on interrupt char */
  412. #else
  413.                (void) signal(SIGINT, inter);
  414. #endif
  415. #endif
  416. #endif
  417. #endif
  418. #else /* MSDOS */
  419.         (void) signal(SIGINT, inter);    /* go there on interrupt char */
  420. #endif /* MSDOS */
  421. }
  422.  
  423.  
  424. /* Look for a gnuplot start-up file */
  425. load_rcfile()
  426. {
  427.     register FILE *plotrc;
  428.     char home[80]; 
  429.     char rcfile[sizeof(PLOTRC)+80];
  430.  
  431.     /* Look for a gnuplot init file in . or home directory */
  432. #ifdef vms
  433.     (void) strcpy(home,HOME);
  434. #else /* vms */
  435.     char *tmp_home=getenv(HOME);
  436.     char *p;    /* points to last char in home path, or to \0, if none */
  437.     char c='\0';/* character that should be added, or \0, if none */
  438.  
  439.  
  440.     if(tmp_home) {
  441.         strcpy(home,tmp_home);
  442.     if( strlen(home) ) p = &home[strlen(home)-1];
  443.     else           p = home;
  444. #if defined(MSDOS) || defined(ATARI) || defined( OS2 ) || defined(_Windows) || defined(DOS386)
  445.     if( *p!='\\' && *p!='\0' ) c='\\';
  446. #else
  447. #if defined(AMIGA_AC_5)
  448.     if( *p!='/' && *p!=':' && *p!='\0' ) c='/';
  449. #else /* that leaves unix */
  450.     c='/';
  451. #endif
  452. #endif
  453.     if(c) {
  454.         if(*p) p++;
  455.         *p++=c;
  456.         *p='\0';
  457.     }
  458.     }
  459. #endif /* vms */
  460.  
  461. #ifdef NOCWDRC
  462.     /* inhibit check of init file in current directory for security reasons */
  463.     {
  464. #else
  465.     (void) strcpy(rcfile, PLOTRC);
  466.     plotrc = fopen(rcfile,"r");
  467.     if (plotrc == (FILE *)NULL) {
  468. #endif
  469. #ifndef vms
  470.     if( tmp_home ) {
  471. #endif
  472.        (void) sprintf(rcfile, "%s%s", home, PLOTRC);
  473.        plotrc = fopen(rcfile,"r");
  474. #ifndef vms
  475.     } else
  476.         plotrc=NULL;
  477. #endif
  478.     }
  479.     if (plotrc)
  480.      load_file(plotrc, rcfile);
  481. }
  482.